package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.common.web.SessionUnit;
import com.sbl.bajw.pc.model.CameraHouseModel;
import com.sbl.bajw.pc.model.CameraHouseSelectModel;
import com.sbl.bajw.pc.model.DeptModel;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.service.CameraHouseService;
import com.sbl.bajw.pc.service.DeptService;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.util.DateUtil;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/CameraHouse")
public class CameraHouseAction {

	@Resource
	private CameraHouseService service;
	
	@Resource
	private DeptService deptService;
	
	@Resource
	private LogTModelService logService;
	
	/**
	 * 监控室采集数据入库
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/createApp")
	public @ResponseBody String createApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String userId = RequestUnit.getString("user_id");
		CameraHouseModel model = RequestUnit.getModel(CameraHouseModel.class);
		model.setCreater(userId);
		model.setIsQuelity("0");
		service.insert(model);
		
		LogTModel logT = new LogTModel();
		logT.setUserid(userId);
		logT.setCode("15");
		logService.insert(logT);
		
		String json = JsonUtil.ConvertObjectToJson(model);
		return JsonResultUtil.Success(json);
	}
	
	/**
	 * 监控室列表查询
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getlist")
	public @ResponseBody String getlist(HttpServletRequest request, HttpServletResponse response){

		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		parm.put("user_id", SessionUnit.getUserId());
		String result = service.getList(parm);
		return result;
	} 
	
	/**
	 * 监控室列表数据导出
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/downloadCameraHouse")
	public void downloadCameraHouse(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		parm.put("user_id", SessionUnit.getUserId());
		
		List<CameraHouseModel> list = service.downloadCameraHouseList(parm);
		for(int i = 0; i < list.size(); i++){
			if("0".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("未审核");
			}else if("-1".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("不合格");
			}else if("1".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("合格");
			}else if("2".equals(list.get(i).getIsQuelity())){
				list.get(i).setIsQuelity("提交再审核");
			}
		}
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("code", "编码");
		column.put("name", "监控室名称");
		column.put("address", "监控室地址");
		column.put("lon", "经度");
		column.put("lat", "纬度");
		column.put("chargeMan", "责任人");
		column.put("phone", "联系电话");
		column.put("cameraNum", "摄像头数量");
		column.put("buildingCode", "楼栋编码");
		column.put("watchManNum", "持证上岗人数");
		column.put("createTime", "采集时间");
		column.put("note", "备注");
		column.put("isQuelity", "是否合格");
		column.put("policeStationName", "派出所");
		column.put("communityName", "社区");
		column.put("creatername", "采集人");
		column.put("auditname", "审核人");
		column.put("audittime", "审核时间");
		column.put("reason", "审核原因");
		column.put("audituserno", "采集人账号");
		column.put("auditusercardid", "采集人身份证号");
		column.put("cameraNormalNum", "正常使用摄像头数量");
		column.put("updater", "更新人");
		column.put("updatetime", "更新时间");
		column.put("installDateStr", "安装时间");
		column.put("createdeptname", "采集单位");
		HSSFWorkbook wb = ExcelUtil.createMExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=CameraHouse.xls");  
		wb.write(outStream);
	} 
	
	/**
	 * APP获取监控室列表数据
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getlistApp")
	public @ResponseBody String getlistApp(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String start = DateUtil.getMondayDate();
		String stop = DateUtil.getSundayDate();
		parm.put("startdate", start + " 00:00:00");
		parm.put("stopdate", stop + "23:59:59");
		
		List<HashMap<String, String>> list = service.getListApp(parm);
		String json = JsonResultUtil.Success(JsonUtil.ConvertArrayToJson(list));
//		int num = service.getCountApp(parm);
//		String result = "{\"num\":" + num + ",\"list\":" + JsonUtil.ConvertArrayToJson(list) + "}";
//		String json = JsonResultUtil.Success("", JsonUtil.ConvertObjectToJson(result));
		return json;
	} 
	
	/**
	 * APP摄像头采集中监控室选择接口
	 * @作者：lrq
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getHouseListForCameraApp")
	public @ResponseBody String getHouseListForCameraApp(HttpServletRequest request, HttpServletResponse response){

		String userid = RequestUnit.getString("user_id");
		List<CameraHouseSelectModel> list = service.getHouseListForCamera(userid);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	} 
	
	@RequestMapping(value = "/List")
	public ModelAndView list(){
		ModelAndView mv = new ModelAndView("/view/manager/camerahouse/List");
		return mv;
	}
	
	@RequestMapping(value = "/detail")
	public ModelAndView detail(){
		String id = RequestUnit.getString("id");
		CameraHouseModel model = service.getModel(id);
		RequestUnit.setAttr("model", model);
		List<HashMap<String, String>> pics = service.getPics(id);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		
		ModelAndView mv = new ModelAndView("/view/manager/camerahouse/detail");
		return mv;
	}
	
	/**
	 * APP查看监控室详情
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/detailApp")
	public @ResponseBody String detailApp(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		
		List<HashMap<String, String>> pics = service.getPics(id);
		CameraHouseModel model = service.getModel(id);
		
		String result = "{\"image\":" + JsonUtil.ConvertArrayToJson(pics) + ",\"data\":" + JsonUtil.ConvertObjectToJson(model) + "}";
		return JsonResultUtil.Success("", result);
	}
	
	/**
	 * APP修改监控室信息
	 * @作者：罗琦
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/updateApp")
	public @ResponseBody String updateApp(HttpServletRequest request, HttpServletResponse response) throws Exception{
		CameraHouseModel model = RequestUnit.getModel(CameraHouseModel.class);
		String userId = RequestUnit.getString("user_id");
		model.setUpdater(userId);
		service.updateApp(model);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/UpdateView")
	public ModelAndView UpdateView(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String id = RequestUnit.getString("id");
		CameraHouseModel model = service.getModel(id);
		List<DeptModel> polices = deptService.getOrgPid("1");
		List<DeptModel> communitys = null;
		List<HashMap<String, String>> pics = service.getPics(model.getId());
		if(model.getPoliceStation() != null){
			communitys = deptService.getOrgPid(model.getPoliceStation());
		}
		else{
			communitys = deptService.getOrgPid(polices.get(0).getId());
		}
		
		RequestUnit.setAttr("polices", polices);
		RequestUnit.setAttr("communitys", communitys);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		ModelAndView mv = new ModelAndView("/view/manager/camerahouse/main");
		return mv;
	}
	
	@RequestMapping(value = "/update")
	public @ResponseBody String update(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String lonr = request.getParameter("lon");
		String lonu = RequestUnit.getParameter("lon");
		
		LogUtil.getLogger("CameraHouse").info("修改检测：request:"+lonr);
		LogUtil.getLogger("CameraHouse").info("修改检测：___unit:" + lonu);
		
		CameraHouseModel model = RequestUnit.getModel(CameraHouseModel.class);
		model.setUpdater(SessionUnit.getUserId());
		service.update(model);
		return JsonResultUtil.Success();
	}
	
	@RequestMapping(value = "/detailInMap")
	public ModelAndView detailInMap(){
		String id = RequestUnit.getString("id");
		List<HashMap<String, String>> pics = service.getPics(id);
		CameraHouseModel model = service.getModel(id);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", pics);
		
		ModelAndView mv = new ModelAndView("/view/manager/camerahouse/detailInMap");
		return mv;
	}
	
	/**
	 * 统计总数、每日采集数、未审核总数以及审核通过总数
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getStatisticOnDay")
	public @ResponseBody String getStatisticOnDay(HttpServletRequest request, HttpServletResponse response){
		HashMap<String, Object> map = service.getStatisticOnDay();
		String json = JsonUtil.ConvertObjectToJson(map);
		return json;
	}
	
	@RequestMapping(value = "/mapView")
	public ModelAndView mapView(){
		List<CameraHouseModel> models = service.getModelsByIds();
		RequestUnit.setAttr("models", models);
		ModelAndView mv = new ModelAndView("/view/manager/camerahouse/mapView");
		return mv;
	}
	
	@RequestMapping(value = "/getCenterCount")
	public @ResponseBody String getCenterCount(HttpServletRequest request, HttpServletResponse response){
		
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		
		List<HashMap<String, String>> list = service.getCenterCount(parm);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
	
	@RequestMapping(value = "/getModelsForMap")
	public @ResponseBody String getModelsForMap(HttpServletRequest request, HttpServletResponse response){
		
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		
		List<HashMap<String, String>> list = service.getModelsForMap(parm);
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(list));
		return json;
	}
}
